<?php

namespace app\store\model;

use think\Model;

/**日志搜索模型
 * Class SearchLogModel
 * @package app\store\model
 */
abstract class SearchLogModel extends Model
{
    protected $connection = 'db_platform_log';

    protected $table = 'user_score_log';

    // 搜索的时间类型
    protected $time_field = null;

    // 搜索的时间
    protected $date_range = null;

    // 搜索的用户ids(注册或者登录用户二者选其一)子查询语句
    protected $user_sub_sql = null;

    // 充值的用户ids子查询(有可能用到的充值)
    protected $trade_sub_sql = null;

    // 筛选的用户字段和值
    protected $user_field = null;
    protected $user_value_start = null;
    protected $user_value_end = null;

    // 用于查询的where条件 根据用户搜索动态变化
    protected $where = null;

    // 固定的搜索条件 每个子类模型根据业务应该有不同的条件
    protected $condition = null;

    /**
     * @param array $data
     * @throws \think\exception\DbException
     */
    public function __construct($data = [])
    {
        parent::__construct($data);

        // 如果传来了空 或者 没有传值 都按照为空字符串的情况处理
        $time_field = request()->get('time_field', '');
        $time_value = request()->get('time_value', '');

        // 获取具体时间 单天
        if (!empty($time_field) and !empty($time_value)) {
            $this->date_range = [$time_value, $time_value . ' 23:59:59'];
        } else {
            $this->date_range = [date('Y-m-d'), date('Y-m-d 23:59:59')];
        }

        // 根据获取筛选的时间类型（是注册还是登录）获取对应的用户 的子查询
        if (empty($time_field) or $time_field == 'RegisterDate') {
            $this->time_field = 'RegisterDate';
            $this->user_sub_sql = UserRegModel::getUserIdsSubSql($this->date_range);
        } else {
            $this->time_field = 'LastLoginDate';
            $this->user_sub_sql = UserLoginModel::getUserIdsSubSql($this->date_range);
        }


        $this->user_field = request()->get('user_field', '');
        $this->user_value_start = request()->get('user_value_start', '');
        $this->user_value_end = request()->get('user_value_end', '');

        if ($this->user_field != '') {

            if ($this->user_value_end != '' or $this->user_value_start != '') {

                // 如果只传了一个值 就把这个值赋给另外一个值
                if ($this->user_value_start != '' and $this->user_value_end == '') {
                    $this->user_value_end = $this->user_value_start;
                } elseif ($this->user_value_start == '' and $this->user_value_end != '') {
                    $this->user_value_start = $this->user_value_end;
                }

                // 1.开始和结束不相等 按正常逻辑走 between处理
                // 2.开始和结束相等(传值时开始就和结束相等 或者 只传了其中一个值 或者 都传了空) 并且不为空 按等于处理
                if ($this->user_value_start == $this->user_value_end
                    and $this->user_value_start != '') {
                    if ($this->user_field == 'cannon') {
                        $this->where['g.Cannon'] = $this->user_value_start;
                    } else {
                        $this->where['a.' . $this->user_field] = $this->user_value_start;
                    }
                } else if ($this->user_value_start != $this->user_value_end) {
                    if ($this->user_field == 'cannon') {
                        $this->where[] = ['g.Cannon', 'between', [$this->user_value_start, $this->user_value_end]];
                    } else {
                        $this->where[] = ['a.' . $this->user_field, 'between', [$this->user_value_start, $this->user_value_end]];
                    }
                }
            }
        }
    }


    /**获取搜索结果
     * @return array
     */
    abstract public function search();

    /**获取query查询
     * @return mixed
     */
    abstract protected function getDbQuery();
}